/********************************************************************
文件名称:   UartDebug.c

文件功能:   环形数组库

文件说明:   该函数库从VxWorks rngLib.c下移植,当在单片机下使用时可能会申请失败
            可以通过在单片机启动汇编文件中修改堆大小解决。

当前版本:   V1.0

修改记录:   2016-10-27  V1.0    王  明  创建
********************************************************************/
#include "UartDebug.h"

/* UART handler declaration */
UART_HandleTypeDef UartHandle;

/* Private function prototypes -----------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

/********************************************************************
函数名称:   rngCreate

函数功能:   rngCreate - create an empty ring buffer

参数名称        类型            输入/输出           含义
nbytes          size_t          input               number of bytes in ring buffer

返回值  :   The ID of the ring buffer, or NULL if memory cannot be allocated.

函数说明:   This routine creates a ring buffer of size <nbytes>, and initializes
            it.  Memory for the buffer is allocated from the system memory parti
            tion.

修改记录:   2016-10-27  V1.0    王  明  创建
********************************************************************/
void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
    /*##-1- Reset peripherals ##################################################*/
    USARTx_FORCE_RESET();
    USARTx_RELEASE_RESET();

    /*##-2- Disable peripherals and GPIO Clocks #################################*/
    /* Configure UART Tx as alternate function  */
    HAL_GPIO_DeInit(USARTx_TX_GPIO_PORT, USARTx_TX_PIN);
    /* Configure UART Rx as alternate function  */
    HAL_GPIO_DeInit(USARTx_RX_GPIO_PORT, USARTx_RX_PIN);
}

/********************************************************************
函数名称:   rngCreate

函数功能:   rngCreate - create an empty ring buffer

参数名称        类型            输入/输出           含义
nbytes          size_t          input               number of bytes in ring buffer

返回值  :   The ID of the ring buffer, or NULL if memory cannot be allocated.

函数说明:   This routine creates a ring buffer of size <nbytes>, and initializes
            it.  Memory for the buffer is allocated from the system memory parti
            tion.

修改记录:   2016-10-27  V1.0    王  明  创建
********************************************************************/
int UartPrintfInit(void)
{
    GPIO_InitTypeDef  GPIO_InitStruct;

    /*##-1- Enable peripherals and GPIO Clocks #################################*/
    /* Enable GPIO TX/RX clock */
    USARTx_TX_GPIO_CLK_ENABLE();
    USARTx_RX_GPIO_CLK_ENABLE();

    /* Enable USARTx clock */
    USARTx_CLK_ENABLE();

    /*##-2- Configure peripheral GPIO ##########################################*/
    /* UART TX GPIO pin configuration  */
    GPIO_InitStruct.Pin       = USARTx_TX_PIN;
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = USARTx_TX_AF;
    HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);

    /* UART RX GPIO pin configuration  */
    GPIO_InitStruct.Pin = USARTx_RX_PIN;
    GPIO_InitStruct.Alternate = USARTx_RX_AF;
    HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);


    UartHandle.Instance        = USARTx;
    UartHandle.Init.BaudRate   = 115200;
    UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
    UartHandle.Init.StopBits   = UART_STOPBITS_1;
    UartHandle.Init.Parity     = UART_PARITY_NONE;
    UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
    UartHandle.Init.Mode       = UART_MODE_TX_RX;
    if (HAL_UART_Init(&UartHandle) != HAL_OK)
    {
        /* Initialization Error */
        //Error_Handler();
    }

    return 0;
}

/********************************************************************
函数名称:   rngCreate

函数功能:   rngCreate - create an empty ring buffer

参数名称        类型            输入/输出           含义
nbytes          size_t          input               number of bytes in ring buffer

返回值  :   The ID of the ring buffer, or NULL if memory cannot be allocated.

函数说明:   This routine creates a ring buffer of size <nbytes>, and initializes
            it.  Memory for the buffer is allocated from the system memory parti
            tion.

修改记录:   2016-10-27  V1.0    王  明  创建
********************************************************************/
PUTCHAR_PROTOTYPE
{
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART3 and Loop until the end of transmission */
    HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);

    return ch;
}

#ifdef  USE_FULL_ASSERT
/********************************************************************
函数名称:   rngCreate

函数功能:   rngCreate - create an empty ring buffer

参数名称        类型            输入/输出           含义
nbytes          size_t          input               number of bytes in ring buffer

返回值  :   The ID of the ring buffer, or NULL if memory cannot be allocated.

函数说明:   This routine creates a ring buffer of size <nbytes>, and initializes
            it.  Memory for the buffer is allocated from the system memory parti
            tion.

修改记录:   2016-10-27  V1.0    王  明  创建
********************************************************************/
void assert_failed(uint8_t *file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {
    }
}
#endif
